package guoanmaker.operator.business.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.OperatorNewsType;
import guoanmaker.operator.business.modelnobind.OperatorNewsTypeMove;
import guoanmaker.operator.business.service.OperatorNewsTypeServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.OPERATOR_NEWTYPE)
public class OperatorNewTypeController {
	@Autowired
	private  AjaxJson ajaxJson;
	@Autowired
	private OperatorNewsTypeServiceInterface operatorNewsTypeServiceInterface;
	@ApiOperation(value = "保存新闻分类", notes = "保存新闻分类，保存新闻分类 ")
    @PostMapping(value = "saveNewType")
    public AjaxJson saveNewType(@RequestBody OperatorNewsType operatorNewsType) {
		  operatorNewsType.setStatus(0);
		  operatorNewsType.setCreateDate(new Date());
		  try {
			  OperatorNewsType oh=operatorNewsTypeServiceInterface.saveOperatorNewsType(operatorNewsType);
			  if(null!=oh){
			        ajaxJson.setSuccess(true).setStatus(200).setData(oh);
	          }else{
	        	  ajaxJson.setSuccess(true).setStatus(230).setData("该原因名称已存在");
	          }
		  } catch (Exception e) {
			  e.printStackTrace();
			  ajaxJson.setSuccess(true).setStatus(250).setData("插入失败");
		  }
        return ajaxJson;
    }
	
	
	 @ApiOperation(value = "分页查询新闻类型", notes = "分页查询新闻类型")
	  @PostMapping(value = "getAllNewsTypeByPage")
	  public AjaxJson getAllNewsTypeByPage(Integer page,Integer size,String newsTypeName) {
		 try {
			 Map<String,Object> map= operatorNewsTypeServiceInterface.getAllnewTypeByPage(page, size,newsTypeName);
			  int a=(int) map.get("count");
			  if(a>0){
		        ajaxJson.setSuccess(true).setStatus(200).setData(map);
		        }else{
		        	ajaxJson.setSuccess(false).setStatus(250).setData("没有查询出数据");
		        }
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(250).setData("没有查询出数据");
		}
		  return  ajaxJson;
	    }
	 
	 
	 @ApiOperation(value = "删除新闻类型", notes = "根据Id删除新闻类型")
	    @PostMapping(value = "deleteNews")
	   public AjaxJson deleteNewsType(String id) {
		 try{
			 int a=operatorNewsTypeServiceInterface.deleteNewsTypeListById(id);
			 if(a==-1){
				 ajaxJson.setSuccess(true).setStatus(230).setData("删除类别中包含已使用的，请刷新重试!");
			 }
			 else{
				 ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
			 }
		 }catch(Exception e){
			 e.printStackTrace();
			 ajaxJson.setSuccess(false).setStatus(250).setData("数据删除失敗!");
		 }
	        return ajaxJson;
	    }
	 @ApiOperation(value = "查询新闻种类 列表", notes = "查询新闻种类 列表")
	 @PostMapping(value = "getAllNewsTypeList")
	 public AjaxJson getAllNewsTypeList() {
		 try {
			 List<OperatorNewsTypeMove> list=operatorNewsTypeServiceInterface.getAllNewsTypeList();
			 if(list!=null&&list.size()>0){
				 ajaxJson.setSuccess(true).setStatus(200).setData(list);
			 }
			 else{
				 ajaxJson.setSuccess(true).setStatus(230).setData("没有数据");
			 }
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(250).setData("数据删除失敗!");
		}
		 return ajaxJson;
	 }
	 @ApiOperation(value = "修改种类名称", notes = "修改种类名称")
	 @PostMapping(value = "updateNewsType")
	 public AjaxJson updateNewsType(String newsTypeName,String id) {
		 try {
			 int a=operatorNewsTypeServiceInterface.updateNewsType(newsTypeName,id);
			 if(a==-1){
				 ajaxJson.setSuccess(true).setStatus(230).setData("该名称已存在");
			 }
			 else{
				 ajaxJson.setSuccess(true).setStatus(200).setData(a);
			 }
		 } catch (Exception e) {
			 e.printStackTrace();
			 ajaxJson.setSuccess(false).setStatus(250).setData("修改失败");
		 }
		 return ajaxJson;
	 }
}
